local MeleeSkill = MClass("MeleeSkill")
local battleCfg = battle.BattleConfig:getInstance()


function MeleeSkill:ctor()
    --self.root 默认为根节点
    --设置事件
    self.btn = self:findChild("Panel/btn_skill/img")
    self.icon = self:findChild("Panel/icon")
    self:init()
end

function MeleeSkill:init()
	BattleUIEvent:addEventListener(BattleUIEvent.TYPE.MELEE_PROP,handler(self,MeleeSkill.setProp))
	local function touchEvent_shop(sender, eventType)
		print(self.coolDown)
		if self.coolDown < 1 then
			return
		end
        if eventType == ccui.TouchEventType.ended then
        	if self.state then
        		BattleUIEvent:dispatchEvent(BattleUIEvent.TYPE.USE_ITEM,{instanceId = self.id,skillId = self.skillId})
        	else
        		BattleUIEvent:dispatchEvent(BattleUIEvent.TYPE.SHOW_BATTLE_SHOP, 1)
        	end
        	self.coolDown = 0
        end
    end
    self.btn:addTouchEventListener(touchEvent_shop)
    self.btn:setTouchEnabled(true)
    self.btn:setVisible(true)
    self.coolDown = 0
end

function MeleeSkill:removeEvent()
	BattleUIEvent:removeEvent(BattleUIEvent.TYPE.MELEE_PROP)
end

function MeleeSkill:setProp(money,buffs,skills,soul,unit)
	self.coolDown = self.coolDown+0.1
	if #skills > 0 then
		--self.btn:setVisible(false)
		self.icon:setVisible(true)
		self.cfg = battleCfg:getItemConfig(unit:getItemIdByInstanceID(skills[1]))
		--self.price = self.cfg:getItemPrice()
		self.iconPic = self.cfg:getItemIcon()
		--self.name = L(self.cfg:getItemName())
		self.icon:loadTexture(string.format(Res.meleeIconPath, self.iconPic))
		self.state = true
		self.id = skills[1]
		self.skillId = self.cfg:getItemSkillID()
	else
		--self.btn:setVisible(true)
		self.icon:setVisible(false)
		self.state = false
	end
end

return MeleeSkill